package com.kintreda.ieco_server.module.order.service.impl;

import com.kintreda.ieco_server.bean.order.UsageRecord;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.module.order.service.IUsageRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.order.service.impl
 * @Author ZhongYao_H
 * @Date 2019/5/4:17:20
 * @Year 2019 Month 05 Day 04
 * @Date_zh 2019 年 五月 星期六
 */
@Service
public class UsageRecordServiceImpl extends BaseServiceImpl<UsageRecord,String> implements IUsageRecordService {

    @Autowired
    IBaseRepository<UsageRecord,String> usageRecordRepository;
    @Autowired
    IUserService userService;

    @Override
    public List<Map<String, Object>> limtQuery(Integer page, Integer rows, String code,String status) {
        List<Object> params = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT t1.*,t2.name as name , t2.mobile as mobile FROM usagerecord t1 , common_user t2 where t1.userOrOrgid = t2.id ");
        if (StringUtils.isNotBlank(code)){
                sql.append("and t1.qrCode = ?");
                params.add(code);
        }
        if (StringUtils.isNotBlank(status)){
                sql.append(" and t1.status = ? ");
                params.add(status);
        }
        sql.append(" ORDER BY createDate DESC ");
        sql.append(" LIMIT ? , ? ");
        if (page==1){
            params.add(0);
        }else {
            params.add(page*rows);
        }
        params.add(rows);
        List<Map<String, Object>> result = usageRecordRepository.findListBySql(sql.toString(), params.toArray());
        return result;
    }

    @Override
    public UsageRecord getByQrcode(String qrcode, String status) {
        List<UsageRecord> result = query("from UsageRecord where qrCode = ? and status = ? ", new Object[]{qrcode, status});
        return result.size()>0?result.get(0):null;
    }

    @Override
    public UsageRecord getByQrcode(String qrcode, String status, String status2) {
        List<UsageRecord> result = query("from UsageRecord where qrCode = ? and (status = ? or status = ?) ", new Object[]{qrcode, status,status2});
        return result.size()>0?result.get(0):null;
    }

    @Override
    public Long countUse(String userId) {
        Long uses = usageRecordRepository.countBySql("select count(*) from usagerecord where userOrOrgid = ? and (status = -2  OR status = 0)", new Object[]{userId});
        return uses;
    }

    @Override
    public List<Map<String,Object>> queryByStatus(String status ,String userId) {
        List<Map<String, Object>> list = usageRecordRepository.findListBySql("select * from usagerecord where status = ? and userOrOrgid = ? order by createDate desc limit 0 ,100", new Object[]{status, userId});
//        List<UsageRecord> query = query("from UsageRecord where status = ? and userOrOrgid = ? order by createDate desc limit 0 ,100 ", new Object[]{status, userId});
        return list;
    }

    @Override
    public List<Map<String, Object>> queryOrgUsageRecordList(String orgId) {
        List<Map<String, Object>> userList = usageRecordRepository.findListBySql("select id  from common_user where orgId = ? ", new Object[]{orgId});
        List<Map<String, Object>> usagerecordList = new LinkedList<>();
        for (Map<String, Object> map : userList) {
            List<Map<String, Object>> userUsageRecordList = queryByStatus("0", map.get("id").toString());
            for (Map<String, Object> usageRecord : userUsageRecordList) {
                usagerecordList.add(usageRecord);
            }

        }
        return usagerecordList;
    }

    @Override
    public List<Map<String, Object>> queryUserUsageRecordList(String orgId) {
        List<Map<String, Object>> userList = usageRecordRepository.findListBySql("select adminId  from organization where id = ? ", new Object[]{orgId});
        List<Map<String, Object>> usagerecordList = new LinkedList<>();
        for (Map<String, Object> map : userList) {
            List<Map<String, Object>> userUsageRecordList = queryByStatus("0", map.get("adminId").toString());
            for (Map<String, Object> usageRecord : userUsageRecordList) {
                usagerecordList.add(usageRecord);
            }

        }
        return usagerecordList;
    }
}
